﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.FTPClient;
namespace WPFExcelReport
{
    public partial class GreigeMasterReport
    {
        private void FillInWarpBeam(string TkeyType, DataRow dwWarpBeam)
        {

            Queue CellQueue = new Queue();

            string beamposition = "";
            string PatternPre = "W_P";
            string RepeatePre = "W_R";
            string PatternField = "WeavingBeamPatternString";
            string BeamArticle = "";
            string BeamVar = "";
            string BeamColor = "";
            string BeamComments = "";
            string Cellname = "";
            string Value = "";
            #region WeavingBeamKey
            #region WS31
            if (TkeyType == "WS31")
            {
                beamposition = dwWarpBeam["WAKPOS"].ToString();
                beamposition = Right("00" + (10 + Convert.ToInt16(beamposition)).ToString(), 2);
                PatternPre = "W_P" + beamposition;
                RepeatePre = "W_R" + beamposition;
                BeamArticle = dwWarpBeam["WAKKART"].ToString();
                BeamVar = dwWarpBeam["WAKKVAR"].ToString();
                BeamColor = GetGB2312Value(dwWarpBeam["WAKKFARBE"].ToString());
                CellQueue.Enqueue("WarpBeamArticle" + beamposition);
                CellQueue.Enqueue("WAKKART");
                CellQueue.Enqueue("WarpBeamVar" + beamposition);
                CellQueue.Enqueue("WAKKVAR");
                CellQueue.Enqueue("WarpBeamColor" + beamposition);
                CellQueue.Enqueue("WAKKFARBE");
                CellQueue.Enqueue("WeavingBeamFactor" + beamposition);
                CellQueue.Enqueue("WAKEAFAKTR");
                CellQueue.Enqueue("WarpBeamPosition" + beamposition);
                CellQueue.Enqueue("WAKPOS");
            }
            #endregion

            #region BS09
            if (TkeyType == "BS09")
            {
                beamposition = dwWarpBeam["BAKPOS"].ToString();
                beamposition = Right("00" + Convert.ToInt16(beamposition).ToString().Trim(), 2);
                PatternPre = "W_P" + beamposition;
                RepeatePre = "W_R" + beamposition;
                BeamArticle = dwWarpBeam["BAKKART"].ToString();
                BeamVar = dwWarpBeam["BAKKVAR"].ToString();
                BeamColor = dwWarpBeam["BAKKFARBE"].ToString();
                CellQueue.Enqueue("WarpBeamPattern" + beamposition);
                CellQueue.Enqueue("BAKPATT");
                CellQueue.Enqueue("WarpBeamEnds" + beamposition);
                CellQueue.Enqueue("BAKKART");
                CellQueue.Enqueue("WarpBeamWarpPattern" + beamposition);
                CellQueue.Enqueue("BAKKART");
                CellQueue.Enqueue("WarpBeamArticle" + beamposition);
                CellQueue.Enqueue("BAKKART");
                CellQueue.Enqueue("WarpBeamVar" + beamposition);
                CellQueue.Enqueue("BAKKVAR");
                CellQueue.Enqueue("WarpBeamColor" + beamposition);
                CellQueue.Enqueue("BAKKFARBE");
                CellQueue.Enqueue("WarpBeamFactor" + beamposition);
                CellQueue.Enqueue("BAKANZ");
                CellQueue.Enqueue("WarpBeamPosition" + beamposition);
                CellQueue.Enqueue("BAKPOS");
            }
            #endregion
            FillInCellbyQueue(CellQueue, dwWarpBeam);
            #endregion

            #region WeavingBeamColorInfor
            var query = from dwBSFARB in dsSource.Tables["BSFARB"].AsEnumerable()
                        where dwBSFARB.Field<string>("BSFKART") == BeamArticle
                        && dwBSFARB.Field<string>("BSFKVAR") == BeamVar
                        && dwBSFARB.Field<string>("BSFKFARBE") == BeamColor
                        select dwBSFARB;
            CellQueue.Clear();
            foreach (DataRow dwQuery in query)
            {
                //WarpBeamPattern01	01	01	01	01	01	01	WarpBeamPatternNameWarpPattern	01	01																																												
                CellQueue.Enqueue("BeamColorDes" + beamposition);
                CellQueue.Enqueue("BSFBEZ");
                FillInCellbyQueue(CellQueue, dwQuery);
                Cellname = "BeamComments" + beamposition;
                Value = dwQuery["COMMENTS"].ToString().Trim().Replace("\n", "").Replace(" ", "");
                /*FillInCellbyQueue(CellQueue, dwQuery);*/
                exAgent.SetCellValue(Cellname, Value, true);
            }
            #endregion

            #region WeavingBeamBase
            DataRow dwBHSATZ = dsSource.Tables["BHSATZ"].Rows[0];
            string aenb = dwBHSATZ["BHAENDBEN"].ToString().Trim();
            if (aenb != "")
            {
                if (aenb != WANEUB)
                {
                    if (!WAAENB.Contains(aenb + ";"))
                    {
                        WAAENB += aenb + ";";
                    }
                }
            }
            var queryWeavingBeamBase = from dwBSSATZ in dsSource.Tables["BSSATZ"].AsEnumerable()
                                       where dwBSSATZ.Field<string>("BSKART") == BeamArticle
                                       select dwBSSATZ;
            CellQueue.Clear();
            string WarpBeamPatternName = "";

            foreach (DataRow dwQuery in queryWeavingBeamBase)
            {
                if (TkeyType == "WS31")
                {
                    WarpEnds = System.Convert.ToDouble(dwQuery["BSFDZ"].ToString());
                }
                CellQueue.Enqueue("WarpBeamEnds" + beamposition);
                CellQueue.Enqueue("BSFDZ");
                CellQueue.Enqueue("WarpBeamPatternName" + beamposition);
                CellQueue.Enqueue("BSSRFNR");
                CellQueue.Enqueue("WarpBeamBase" + beamposition);
                CellQueue.Enqueue("BSFAKTOR");
                WarpBeamEnds11 = System.Convert.ToInt16(dwQuery["BSFDZ"].ToString());
                FillInCellbyQueue(CellQueue, dwQuery);
                WarpBeamPatternName = dwQuery["BSSRFNR"].ToString();
            }
            #endregion

            #region WeavingPattern
            PatternField = "WarpBeamWarpPattern" + beamposition;
            FillInWarpPattern(PatternField, PatternPre, RepeatePre, WarpBeamPatternName, beamposition);
            #endregion

            #region WarpBeam
            var queryWarpBeam = from dwBASTKE in dsSource.Tables["BASTKE"].AsEnumerable()
                                where dwBASTKE.Field<string>("BAKART") == BeamArticle
                                && dwBASTKE.Field<string>("BAKVAR") == BeamVar
                                && dwBASTKE.Field<string>("BAKFARBE") == BeamColor
                                select dwBASTKE;
            foreach (DataRow dwQuery in queryWarpBeam)
            {
                FillInWarpBeam("BS09", dwQuery);
            }

            #endregion
        }
        private void FillInDrafting(DataRow dwWASTPASS)
        {

            Queue CellQueue = new Queue();
            string draftposition = "";
            string DraftNumber = "";
            draftposition = dwWASTPASS["WSPPOS"].ToString();
            draftposition = Right("00" + Convert.ToInt16(draftposition).ToString().Trim(), 2);

            DraftNumber = dwWASTPASS["WSPPASS"].ToString();
            CellQueue.Enqueue("WSPPASS" + draftposition);
            CellQueue.Enqueue("WSPPASS");
            FillInCellbyQueue(CellQueue, dwWASTPASS);
            var query = from dwWTPASS in dsSource.Tables["WTPASS"].AsEnumerable()
                        where dwWTPASS.Field<string>("WTPPASSNR") == DraftNumber
                        select dwWTPASS;
            CellQueue.Clear();
            foreach (DataRow dwQuery in query)
            {
                CellQueue.Enqueue("WTPBEZ" + draftposition);
                CellQueue.Enqueue("WTPBEZ");
                FillInCellbyQueue(CellQueue, dwQuery);
            }
        }
        private void FillInWarpPattern(string PatternField, string PatternPre, string RepeatePre, string BeamPattern, string beamPos)
        {

            string CellName = "";
            string TableName = "";
            string FieldName = "";
            string sRow = "";
            string value = "";
            int wbi = 1;
            int wai = 1;
            Dictionary<string, string> WarpMatrix = PatternMatrix(22, 40, true);
            Array WR = Array.CreateInstance(typeof(Int32), 4, 698);
            Array WM = Array.CreateInstance(typeof(Int32), 100, 698);
            string PatternS = "";
            string YarnEnds = "";
            Queue CellQueue = new Queue();
            Queue WRWRQueue = new Queue();
            string WBi;
            string WBj;
            int maxwbi = 0;
            BaseFunction mbase = new BaseFunction();
            try
            {

                DataTable PatternTableSingle = dsSource.Tables["WTSAER"].Clone();
                DataRow dwPatternTableSingle;
                var queryWeavingBeamBase = from dwWTSAER in dsSource.Tables["WTSAER"].AsEnumerable()
                                           where dwWTSAER.Field<string>("WTBSRFNR") == BeamPattern
                                           select dwWTSAER;

                foreach (DataRow dwWTSAER in queryWeavingBeamBase)
                {
                    YarnEnds = dwWTSAER["WTBSRAPP"].ToString();
                    dwPatternTableSingle = PatternTableSingle.Rows.Add();
                    dwPatternTableSingle.ItemArray = dwWTSAER.ItemArray;
                    PatternTableSingle.AcceptChanges();
                    CellQueue.Clear();
                    CellQueue.Enqueue("WarpBeamCADKEY" + beamPos);
                    CellQueue.Enqueue("WTBCADKEY1");
                    FillInCellbyQueue(CellQueue, dwWTSAER);

                    CellName = "WARPPATCOM" + beamPos;
                    value = dwWTSAER["COMMENTCN"].ToString().Trim();
                    exAgent.SetCellValue(CellName, value, true);

                    //WTBGARN1,WTBFDZ1,   WarpYarnEnds01

                    #region
                    string WTBFNR, WTBANZFAE, WTBWDHFAKTi;
                    string WTBGARN, WTBFDZ, WarpYarnEnds;

                    for (int YNi = 1; YNi <= 100; YNi++)
                    {
                        if (YNi < 10)
                        {
                            WTBGARN = "WTBGARN" + YNi.ToString();
                            WTBFDZ = "WTBFDZ" + YNi.ToString();
                            WarpYarnEnds = "WarpYarnEnds" + mbase.Right("00" + YNi.ToString(), 2);
                            string wends = dwWTSAER[WTBFDZ].ToString().Trim();
                            if (wends != "0")
                            {
                                exAgent.SetCellValue(WarpYarnEnds, dwWTSAER[WTBFDZ].ToString(), true);
                            }
                        }
                        int Ends = 0;
                        WTBFNR = "WTBFNR" + YNi.ToString();
                        WTBANZFAE = "WTBANZFAE" + YNi.ToString();
                        wai = System.Convert.ToInt16(dwWTSAER[WTBFNR].ToString()) - 1;
                        Ends = System.Convert.ToInt16(dwWTSAER[WTBANZFAE].ToString());
                        wbi = YNi - 1;
                        WBi = Right("00" + (wai + 1).ToString(), 2);
                        WBj = Right("000" + (wbi + 1).ToString(), 3);
                        CellName = PatternPre + WBi + WBj;
                        if (wai > -1 && wbi > -1)
                        {
                            WM.SetValue(Ends, wai, wbi);
                        }
                        //exAgent.SetCellValue(CellName, Ends, true);
                        sRow = Right(FieldName, 4);
                        int C = YNi - 1;
                        int RPi = 0;
                        for (int R = 1; R < 5; R++)
                        {
                            WTBWDHFAKTi = "WTBWDHFAKT" + YNi.ToString() + R.ToString();
                            RPi = System.Convert.ToInt16(dwWTSAER[WTBWDHFAKTi].ToString());
                            WR.SetValue(RPi, R - 1, C);
                            WBi = Right("00" + (R).ToString(), 2);
                            WBj = Right("000" + (C + 1).ToString(), 3);
                            sRow = WBi + WBj;
                            CellName = RepeatePre + sRow;
                            value = RPi.ToString();
                            if (RPi == 0)
                            {
                                value = "";
                            }
                            //exAgent.SetCellValue(CellName, value, true);
                        }
                    }
                    #endregion
                }

                TableName = "WTSAER";
                PatternS = BuildPatternString(WR, WM, TableName);
                PatternS = PatternS.Replace("+", "");
                CellName = PatternField;
                exAgent.SetCellValue(CellName, PatternS, true);
                CellName = "WTBSRAPP" + Right(PatternField, 2).ToString();
                exAgent.SetCellValue(CellName, YarnEnds, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInWarpYarnWS31(string TgaType, DataTable dtBastga)
        {

            string Article;
            string Var;
            string Yarn;
            DataTable dtBastke = null;
            string KART = "";
            string KVAR = "";
            string KFARBE = "";
            string KANZ = "";

            dtBastke = dsSource.Tables["WASTKE"];
            KART = "WAKKART";
            KVAR = "WAKKVAR";
            KFARBE = "WAKKFARBE";
            KANZ = "WAKEAFAKTR";

            #region WarpYarn
            var query = from dwBastga in dtBastga.AsEnumerable()
                        let BeamArticle = dwBastga.Field<string>("BAGART") + dwBastga.Field<string>("BAGVAR") + dwBastga.Field<string>("BAGFARBE")
                        join dWBastke in dtBastke.AsEnumerable()
                       on BeamArticle equals dWBastke.Field<string>(KART) + dWBastke.Field<string>(KVAR) + dWBastke.Field<string>(KFARBE)
                        group new { dwBastga, dWBastke } by new
                        {
                            YarnPos = dwBastga["BAGPOS"],
                            YarnArticle = dwBastga["BAGGART"],
                            YarnVar = dwBastga["BAGGVAR"],
                            YarnColor = dwBastga["BAGGFARBE"]
                        }
                            into grp
                            select new
                            {
                                Yarn = grp.Key,
                                TtlBAGKGNET = grp.Sum(g => g.dwBastga.Field<decimal>("BAGKGNET") / g.dWBastke.Field<decimal>(KANZ)),
                                TtlBAGKGBRUT = grp.Sum(g => g.dwBastga.Field<decimal>("BAGKGBRUT") / g.dWBastke.Field<decimal>(KANZ)),
                                TtlEnds = grp.Sum(g => g.dwBastga.Field<decimal>("BAGFDZ")),
                                MaxWastage = grp.Max(g => g.dwBastga.Field<decimal>("BAGABF")),
                            };
            #endregion

            int YarnNum = 0;
            string YarnPosition = "";
            Queue CellQ = new Queue();
            string YarnBase = "";
            string YarnVardes = "";
            foreach (var varYarn in query)
            {
                YarnNum++;
                //YarnPosition = Right("00" + YarnNum.ToString(), 2);
                YarnPosition = varYarn.Yarn.YarnPos.ToString();
                Article = varYarn.Yarn.YarnArticle.ToString();
                Var = varYarn.Yarn.YarnVar.ToString();
                Yarn = varYarn.Yarn.YarnArticle.ToString().Trim() + "/" + varYarn.Yarn.YarnVar.ToString().Trim() + "/" + varYarn.Yarn.YarnColor.ToString().Trim();
                YarnBase = varYarn.Yarn.YarnArticle.ToString().Trim() + "/" + varYarn.Yarn.YarnVar.ToString().Trim();
                Yarn = GetGB2312Value(Yarn);
                DataRow[] dwtemp = dsSource.Tables["GSDAT"].Select("GSART='" + Article + "' AND GSVAR='" + Var + "'");
                if (dwtemp.Count() > 0)
                {
                    DataRow dwt = dwtemp[0];
                    YarnVardes = GetGB2312Value(dwt["GSGARNBEZ"].ToString().TrimEnd());
                };
                CellQ.Enqueue("WarpYarn" + YarnPosition);
                CellQ.Enqueue(Yarn.ToString());
                CellQ.Enqueue("WWarpYarn" + YarnPosition);
                CellQ.Enqueue(Yarn.ToString() + Environment.NewLine + YarnVardes);
                //CellQ.Enqueue(Yarn.ToString());
                CellQ.Enqueue("WarpYarnDes" + YarnPosition);
                CellQ.Enqueue(GetYarnDes(dsSource.Tables["GSCOLOR"], Yarn));
                string TtlBAGKGNET = varYarn.TtlBAGKGNET.ToString();
                string TtlBAGKGBRUT = varYarn.TtlBAGKGBRUT.ToString();
                string TtlEnds = varYarn.TtlEnds.ToString();
                string MaxWastage = varYarn.MaxWastage.ToString();
                Jdense += ComputeDense(Warpdensity, WarpEnds, System.Convert.ToDouble(TtlEnds), YarnBase);
                CellQ.Enqueue("WarpYarnNet" + YarnPosition);
                CellQ.Enqueue(TtlBAGKGNET);
                CellQ.Enqueue("WarpYarnGross" + YarnPosition);
                CellQ.Enqueue(TtlBAGKGBRUT);
                CellQ.Enqueue("WarpYarnTTL" + YarnPosition);
                CellQ.Enqueue(TtlEnds);
                CellQ.Enqueue("MaxWastage" + YarnPosition);
                CellQ.Enqueue(MaxWastage);
            }
            FillInCellbyQueue(CellQ);
            string CellName = "";
            string Cellvalue = "";
            CellName = "Jdense";
            Cellvalue = Jdense.ToString();
            exAgent.SetCellValue(CellName, Cellvalue, true);

        }
        private void FillInWarpYarnBS08(string TgaType, DataTable dtBastga)
        {
            string Yarn;
            DataTable dtBastke = null;
            string KART = "";
            string KVAR = "";
            string KFARBE = "";
            string KANZ = "";


            dtBastke = dsSource.Tables["BASTKE"];
            KART = "BAKKART";
            KVAR = "BAKKVAR";
            KFARBE = "BAKKFARBE";
            KANZ = "BAKANZ";
            #region WarpYarn
            var query = from dwBastga in dtBastga.AsEnumerable()
                        let BeamArticle = dwBastga.Field<string>("BAGART") + dwBastga.Field<string>("BAGVAR") + dwBastga.Field<string>("BAGFARBE")
                        join dWBastke in dtBastke.AsEnumerable()
                       on BeamArticle equals dWBastke.Field<string>(KART) + dWBastke.Field<string>(KVAR) + dWBastke.Field<string>(KFARBE)
                        group new { dwBastga, dWBastke } by new
                        {
                            YarnPos = dwBastga["BAGPOS"],
                            YarnArticle = dwBastga["BAGGART"],
                            YarnVar = dwBastga["BAGGVAR"],
                            YarnColor = dwBastga["BAGGFARBE"]
                        }
                            into grp
                            select new
                            {
                                Yarn = grp.Key,
                                TtlBAGKGNET = grp.Sum(g => g.dwBastga.Field<decimal>("BAGKGNET") * g.dWBastke.Field<decimal>(KANZ)),
                                TtlBAGKGBRUT = grp.Sum(g => g.dwBastga.Field<decimal>("BAGKGBRUT") * g.dWBastke.Field<decimal>(KANZ)),
                                TtlEnds = grp.Sum(g => g.dwBastga.Field<decimal>("BAGFDZ") * g.dWBastke.Field<decimal>(KANZ)),
                                MaxWastage = grp.Max(g => g.dwBastga.Field<decimal>("BAGABF")),
                            };
            #endregion
            int YarnNum = 0;
            string YarnPosition = "";
            Queue CellQ = new Queue();
            string YarnBase = "";

            foreach (var varYarn in query)
            {
                YarnNum++;
                YarnPosition = varYarn.Yarn.YarnPos.ToString();
                Yarn = varYarn.Yarn.YarnArticle.ToString().Trim() + "/" + varYarn.Yarn.YarnVar.ToString().Trim() + "/" + varYarn.Yarn.YarnColor.ToString().Trim();
                YarnBase = varYarn.Yarn.YarnArticle.ToString().Trim() + "/" + varYarn.Yarn.YarnVar.ToString().Trim();
                Yarn = GetGB2312Value(Yarn);
                CellQ.Enqueue("WarpYarn" + YarnPosition);
                CellQ.Enqueue(Yarn.ToString());
                CellQ.Enqueue("WarpYarnDes" + YarnPosition);
                CellQ.Enqueue(GetYarnDes(dsSource.Tables["GSCOLOR"], Yarn));
                string TtlBAGKGNET = varYarn.TtlBAGKGNET.ToString();
                string TtlBAGKGBRUT = varYarn.TtlBAGKGBRUT.ToString();
                string TtlEnds = varYarn.TtlEnds.ToString();
                string MaxWastage = varYarn.MaxWastage.ToString();
                Jdense += ComputeDense(Warpdensity, WarpEnds, System.Convert.ToDouble(TtlEnds), YarnBase);
                CellQ.Enqueue("WarpYarnNet" + YarnPosition);
                CellQ.Enqueue(TtlBAGKGNET);
                CellQ.Enqueue("WarpYarnGross" + YarnPosition);
                CellQ.Enqueue(TtlBAGKGBRUT);
                CellQ.Enqueue("WarpYarnTTL" + YarnPosition);
                CellQ.Enqueue(TtlEnds);
                CellQ.Enqueue("MaxWastage" + YarnPosition);
                CellQ.Enqueue(MaxWastage);
            }
            FillInCellbyQueue(CellQ);
            string CellName = "";
            string Cellvalue = "";
            CellName = "Jdense";
            Cellvalue = Jdense.ToString();
            exAgent.SetCellValue(CellName, Cellvalue, true);

        }
        private void FillInWarpYarn(string TgaType, DataTable dtBastga)
        {
            switch (TgaType)
            {
                case "BS08":
                    FillInWarpYarnBS08(TgaType, dtBastga);
                    break;
                case "WS31":
                    FillInWarpYarnWS31(TgaType, dtBastga);
                    break;
                default:
                    break;
            }
        }
    }
}
